package com.jzg.mvc.api.soft;

import java.io.File;
import java.util.List;

import com.jfinal.core.Path;
import com.jfinal.kit.PathKit;
import com.jfinal.log.Log;
import com.jzg.common.controller.BaseController;
import com.jzg.common.model.biz.Version;
import com.jzg.common.model.biz.VersionFiles;

@Path(value = "/api/soft", viewPath = "")
public class SoftController  extends BaseController{
	protected final Log log = Log.getLog(getClass());
	public void getVersion() {
		Version version=Version.dao.findFirst("select * from "+Version.TABLE_NAME+" where number = (select max(number) from "+Version.TABLE_NAME+") and status='1' ");
		if(version!=null ) {
			List<VersionFiles> versionFiles=VersionFiles.dao.find("select * from "+VersionFiles.TABLE_NAME+" where versionId=?",version.getId());
			if(versionFiles.size()>0) {
				this.renderSuccess("成功!",version);
				return;
			}
		}
		this.renderError("暂无新版本！");
	}
	
	public void getUpdates() {
		Version version=Version.dao.findFirst("select * from "+Version.TABLE_NAME+" where number = (select max(number) from "+Version.TABLE_NAME+")");
		if(version!=null) {
			List<VersionFiles> versionFiles=VersionFiles.dao.find("select * from "+VersionFiles.TABLE_NAME+" where versionId=?",version.getId());
			if(versionFiles!=null && versionFiles.size()>0) {
				this.renderSuccess("成功!",versionFiles);
			}else {
				version.setStatus("0");
				version.update();
				this.renderError("无升级明细！");
			}
		}else {
			this.renderError("无需升级！");
		}
	}
	
	public void down() {
		String fileId=this.getPara("fileId");
		String cabinetId=this.getPara("cabinetId");
		log.debug("柜子升级中="+cabinetId);
		VersionFiles versionFiles=VersionFiles.dao.findById(fileId);
		if(versionFiles!=null) {
			 renderFile(new File(PathKit.getWebRootPath()+ versionFiles.getFilePath()));
		}else {
			this.renderNull();
		}
	}

}
